namespace QMVnSoft.Invoices
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.Controls.ChildGridControls;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormCTKHOBarcodeEditor : FormEditor
    {
        private SearchLotNumberColumn Column1;
        private DataGridViewTextBoxColumn Column10;
        private OptionColumn Column11;
        private DateColumn Column2;
        private NumericColumn Column22;
        private SearchColumn Column3;
        private SearchColumn Column9;
        private IContainer components;
        private GBoxControl gBoxControl3;
        private GBoxControl gBoxControl4;
        private GridDetail grdDetail;
        private bool ib_BarcodeLocked;
        private bool ib_UserKeyPassed;
        private int ii_BarcodePosted;
        private object is_Barcode_Desc_EN;
        private object is_Barcode_Dien_Giai;
        private object is_Barcode_Ma_Dt;
        private object is_Barcode_Ma_Kho;
        private LabelControl lblMa_Dt;
        private LabelControl lblMa_Vt;
        private Label lblNote01;
        private Label lblNote02;
        private CTKHODataAccess oMethod;
        private TextSearch txtMa_Dt;
        private TextControl txtMa_Vt;
        private TextControl txtMa_Vt_Temp;

        public FormCTKHOBarcodeEditor()
        {
            this.oMethod = new CTKHODataAccess();
            this.InitializeComponent();
        }

        public FormCTKHOBarcodeEditor(string ps_InvoiceCode, string ps_MasterInvoiceOrder) : base(ps_InvoiceCode, ps_MasterInvoiceOrder)
        {
            this.oMethod = new CTKHODataAccess();
            this.InitializeComponent();
            base.InvoiceCode = ps_InvoiceCode;
            base.AppInvoiceParameters.ReportFileName = "BarcodeOutInventory";
            base.ToolStripFunctions = null;
            try
            {
                this.BarcodeLocked = short.Parse(AppDataAccess.GetUservar(AppInstance.UserName, "BARCODE_INV_Locked").ToString()) == 1;
                this.BarcodePosted = short.Parse(AppDataAccess.GetUservar(AppInstance.UserName, "BARCODE_INV_Posted").ToString());
                this.Barcode_Ma_Dt = AppDataAccess.GetUservar(AppInstance.UserName, "BARCODE_INV_Ma_Dt").ToString();
                this.Barcode_Dien_Giai = AppDataAccess.GetUservar(AppInstance.UserName, "BARCODE_INV_Dien_Giai").ToString();
                this.Barcode_Desc_EN = AppDataAccess.GetUservar(AppInstance.UserName, "BARCODE_INV_Desc_EN").ToString();
                this.Barcode_Ma_Kho = AppDataAccess.GetUservar(AppInstance.UserName, "BARCODE_INV_Ma_Kho").ToString();
            }
            catch
            {
                MessageBox.Show("Bạn cần kiểm tra lại Thông số khai báo hóa đơn!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            base.DetailGridBrowser.CellEnter += new DataGridViewCellEventHandler(this.DetailGridBrowser_CellEnter);
            base.LocalMasterRow = this.oMethod.GetMasterDataRow(base.SavedMasterOrder);
            base.LocalDetailDataSet = this.oMethod.GetDetailDataSet(base.SavedMasterOrder);
            this.OnAdd();
        }

        private bool AddNewRow(object ps_Ma_Vt)
        {
            if (((ps_Ma_Vt == null) || string.IsNullOrEmpty(ps_Ma_Vt.ToString())) || this.UserKeyPassed)
            {
                return false;
            }
            this.UserKeyPassed = true;
            this.txtMa_Vt.SelectAll();
            DataRow row = AppDataAccess.SelectDataRow(false, "DMVT", "Ma_Vt", ps_Ma_Vt.ToString(), null);
            if (((row == null) && (ps_Ma_Vt.ToString().Length < 6)) && ((base.DetailGridBrowser.CurrentRow != null) && (base.DetailGridBrowser.CurrentRow.DataBoundItem != null)))
            {
                decimal num = 0M;
                try
                {
                    num = Convert.ToDecimal(ps_Ma_Vt);
                }
                catch
                {
                    return false;
                }
                DataRow row2 = ((DataRowView) base.DetailGridBrowser.CurrentRow.DataBoundItem).Row;
                if (AppInstance.LotNumberCtrl)
                {
                    row = AppDataAccess.SelectDataRow(false, "DMVT", "Ma_Vt", row2["Ma_Vt"].ToString(), null);
                    this.LotNumberCalculate(row, row2["Ma_Vt"], num);
                }
                else
                {
                    row2["So_Luong9"] = ((decimal) row2["So_Luong9"]) + num;
                    row2["So_Luong"] = row2["So_Luong9"];
                }
                return false;
            }
            if (row == null)
            {
                MessageBox.Show("Mã hàng hóa không tồn tại!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (AppInstance.LotNumberCtrl)
            {
                this.LotNumberCalculate(row, ps_Ma_Vt, 1M);
            }
            else
            {
                int num2 = 0;
                BindingSource dataSource = base.DetailGridBrowser.DataSource as BindingSource;
                foreach (DataRow row3 in base.LocalDetailTable.Rows)
                {
                    if (row3.RowState != DataRowState.Deleted)
                    {
                        if (((int) row3["Stt_Nv"]) > num2)
                        {
                            num2 = (int) row3["Stt_Nv"];
                        }
                        if (row3["Ma_Vt"].Equals(ps_Ma_Vt))
                        {
                            row3["So_Luong9"] = (decimal) row3["So_Luong9"] + 1;
                            //row3["So_Luong9"] = decimal.op_Increment((decimal)row3["So_Luong9"]);
                            row3["So_Luong"] = row3["So_Luong9"];
                            dataSource.Position = dataSource.Find(base.DetailOrderString, row3[base.DetailOrderString]);
                            return false;
                        }
                    }
                }
                base.DetailGridBrowser.EndEdit();
                DataRow row4 = base.MakeDetailRow();
                int key = num2 + 1;
                if (row["Gia_Ban"] != DBNull.Value)
                {
                    decimal decimal1 = (decimal) row["Gia_Ban"];
                }
                row4["Stt_Nv"] = key;
                row4["He_So9"] = 1;
                row4["Ma_Vt"] = ps_Ma_Vt;
                row4["Ten_Vt"] = row["Ten_Vt"];
                row4["Dvt"] = row["Dvt"];
                row4["Ma_Kho"] = this.Barcode_Ma_Kho;
                row4["So_Luong9"] = 1;
                row4["So_Luong"] = 1;
                row4["H_Luong"] = 0;
                row4["Do_Am_PT"] = 0;
                row4["Do_Con_PT"] = 0;
                base.LocalDetailTable.Rows.Add(row4);
                dataSource.Position = dataSource.Find(base.DetailOrderString, key);
            }
            this.lblNote01.Text = row["Ten_Vt"].ToString();
            this.lblNote02.Text = row["Dvt"].ToString();
            return true;
        }

        private void DetailGridBrowser_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 4)
            {
                DataRow row = ((DataRowView) base.DetailGridBrowser.CurrentRow.DataBoundItem).Row;
                (base.DetailGridBrowser.CurrentCell as SearchLotNumberCell).InvoicePosted = base.LocalMasterRow["Posted"];
                (base.DetailGridBrowser.CurrentCell as SearchLotNumberCell).InvoiceDate = (DateTime) base.txtNgay_Ct.Value;
                (base.DetailGridBrowser.CurrentCell as SearchLotNumberCell).MasterKey = base.SavedMasterOrder;
                (base.DetailGridBrowser.CurrentCell as SearchLotNumberCell).DetailKey = base.SavedDetailOrder;
                (base.DetailGridBrowser.CurrentCell as SearchLotNumberCell).InventoryCode = row["Ma_Kho"];
                (base.DetailGridBrowser.CurrentCell as SearchLotNumberCell).RackNumber = row["Ma_Ke"];
                (base.DetailGridBrowser.CurrentCell as SearchLotNumberCell).LocalItemCode = row["Ma_Vt"];
                (base.DetailGridBrowser.CurrentCell as SearchLotNumberCell).LocalDetailTable = base.LocalDetailTable;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void grdDetail_CellBottomLeftLeaved(object sender, EventArgs e)
        {
            (base.DetailGridBrowser as GridDetail).UserMoveNextCell = GridDetail.MoveNextType.NextCell;
        }

        private void grdDetail_CellRightLeaved(object sender, EventArgs e)
        {
            (base.DetailGridBrowser as GridDetail).UserMoveNextCell = GridDetail.MoveNextType.FirstCellNextRow;
        }

        private void grdDetail_CurrentRowsRemoved(object sender, EventArgs e)
        {
            if (((GridDetail) base.DetailGridBrowser).CurrentSelectionRows != null)
            {
                ((DataRowView) base.DetailGridBrowser.CurrentRow.DataBoundItem).Row.Delete();
            }
        }

        private void InitializeComponent()
        {
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            DataGridViewCellStyle style2 = new DataGridViewCellStyle();
            DataGridViewCellStyle style3 = new DataGridViewCellStyle();
            DataGridViewCellStyle style4 = new DataGridViewCellStyle();
            DataGridViewCellStyle style5 = new DataGridViewCellStyle();
            DataGridViewCellStyle style6 = new DataGridViewCellStyle();
            //ComponentResourceManager manager = new ComponentResourceManager(typeof(FormCTKHOBarcodeEditor));
            this.grdDetail = new GridDetail();
            this.Column9 = new SearchColumn();
            this.Column10 = new DataGridViewTextBoxColumn();
            this.Column11 = new OptionColumn();
            this.Column3 = new SearchColumn();
            this.Column1 = new SearchLotNumberColumn();
            this.Column2 = new DateColumn();
            this.Column22 = new NumericColumn();
            this.txtMa_Vt = new TextControl();
            this.lblMa_Vt = new LabelControl();
            this.lblNote01 = new Label();
            this.lblNote02 = new Label();
            this.gBoxControl3 = new GBoxControl();
            this.gBoxControl4 = new GBoxControl();
            this.lblMa_Dt = new LabelControl();
            this.txtMa_Dt = new TextSearch();
            this.txtMa_Vt_Temp = new TextControl();
            ((ISupportInitialize) this.grdDetail).BeginInit();
            base.SuspendLayout();
            base.lblPhan_Loai.Location = new Point(12, 0x1ec);
            base.txtMa_Loai1.Location = new Point(0x63, 0x1e9);
            base.txtMa_Loai1.TabIndex = 10;
            base.txtMa_Loai2.Location = new Point(0x147, 0x1e9);
            base.txtMa_Loai2.TabIndex = 11;
            base.txtMa_Loai3.Location = new Point(0x22b, 0x1e9);
            base.txtMa_Loai3.TabIndex = 12;
            base.txtSo_Ct.TabIndex = 2;
            base.txtSo_Ct.TabStop = false;
            base.txtNgay_Ps.TabIndex = 0;
            base.txtNgay_Ps.TabStop = false;
            base.txtNgay_Ct.TabIndex = 1;
            base.txtNgay_Ct.TabStop = false;
            this.grdDetail.AllowUserToAddRows = false;
            this.grdDetail.AllowUserToDeleteRows = false;
            this.grdDetail.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.grdDetail.CheckAutoSize = true;
            this.grdDetail.CheckColor = false;
            this.grdDetail.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdDetail.Columns.AddRange(new DataGridViewColumn[] { this.Column9, this.Column10, this.Column11, this.Column3, this.Column1, this.Column2, this.Column22 });
            this.grdDetail.Font = new Font("Arial", 8.25f);
            this.grdDetail.Location = new Point(0, 0x8d);
            this.grdDetail.Name = "grdDetail";
            this.grdDetail.Size = new Size(0x31c, 0x156);
            this.grdDetail.TabIndex = 6;
            this.grdDetail.CurrentRowsRemoved += new EventHandler(this.grdDetail_CurrentRowsRemoved);
            this.grdDetail.CellRightLeaved += new EventHandler(this.grdDetail_CellRightLeaved);
            this.grdDetail.CellBottomLeftLeaved += new EventHandler(this.grdDetail_CellBottomLeftLeaved);
            this.Column9.DataPropertyName = "Ma_Vt";
            style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.Column9.DefaultCellStyle = style;
            this.Column9.DestinationFields = "Ten_Vt, Dvt";
            this.Column9.DisabledForeColor = Color.Blue;
            this.Column9.EmptyIsValid = true;
            this.Column9.HeaderText = "Mã hàng hóa";
            this.Column9.Name = "Column9";
            this.Column9.ReadOnly = true;
            this.Column9.Resizable = DataGridViewTriState.True;
            this.Column9.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column9.SourceFields = "Ten_Vt, Dvt";
            this.Column9.TableName = "DMVT";
            this.Column9.Width = 80;
            this.Column10.DataPropertyName = "Ten_Vt";
            this.Column10.HeaderText = "Tên hàng hóa";
            this.Column10.Name = "Column10";
            this.Column10.ReadOnly = true;
            this.Column10.Width = 0x12d;
            this.Column11.DataPropertyName = "Dvt";
            style2.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.Column11.DefaultCellStyle = style2;
            this.Column11.DisabledForeColor = Color.Blue;
            this.Column11.HeaderText = "Đvt";
            this.Column11.Name = "Column11";
            this.Column11.ReadOnly = true;
            this.Column11.Resizable = DataGridViewTriState.True;
            this.Column11.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column11.UserTextLength = AppInstance.TextControlLength.None;
            this.Column11.Width = 50;
            this.Column3.DataPropertyName = "Ma_Ke";
            style3.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.Column3.DefaultCellStyle = style3;
            this.Column3.DisabledForeColor = Color.Blue;
            this.Column3.HeaderText = "Kệ hàng";
            this.Column3.Name = "Column3";
            this.Column3.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column3.TableName = "DMKE";
            this.Column3.Width = 80;
            this.Column1.DataPropertyName = "Lo_Hang";
            style4.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.Column1.DefaultCellStyle = style4;
            this.Column1.DestinationFields = "Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT";
            this.Column1.DisabledForeColor = Color.Blue;
            this.Column1.HeaderText = "L\x00f4 hàng";
            this.Column1.LocalWorkArea = 2;
            this.Column1.Name = "Column1";
            this.Column1.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column1.SourceFields = "Han_Dung, H_Luong, Do_Am_PT, Do_Con_PT";
            this.Column1.TableName = "LOHANG";
            this.Column1.Width = 80;
            this.Column2.DataPropertyName = "Han_Dung";
            style5.Alignment = DataGridViewContentAlignment.MiddleLeft;
            this.Column2.DefaultCellStyle = style5;
            this.Column2.HeaderText = "Hạn d\x00f9ng";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Resizable = DataGridViewTriState.False;
            this.Column2.Width = 0x3e;
            this.Column22.DataPropertyName = "So_Luong9";
            style6.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column22.DefaultCellStyle = style6;
            this.Column22.HeaderText = "Số lượng";
            this.Column22.Name = "Column22";
            this.Column22.ReadOnly = true;
            this.Column22.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column22.UserTextNumericFormat = AppInstance.TextNumericFormat.Quantity;
            this.Column22.ZeroIsValid = true;
            this.txtMa_Vt.CharacterCasing = CharacterCasing.Upper;
            this.txtMa_Vt.CheckTextSearched = false;
            this.txtMa_Vt.DisabledForeColor = Color.Blue;
            this.txtMa_Vt.FollowedSystemFont = false;
            this.txtMa_Vt.Font = new Font("Arial", 14.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.txtMa_Vt.Location = new Point(0x9a, 0x4e);
            this.txtMa_Vt.MaxLength = 0x20;
            this.txtMa_Vt.Name = "txtMa_Vt";
            this.txtMa_Vt.OldValue = null;
            this.txtMa_Vt.Size = new Size(0xac, 0x1d);
            this.txtMa_Vt.TabIndex = 3;
            this.txtMa_Vt.UserTextLength = AppInstance.TextControlLength.ItemCode;
//            this.txtMa_Vt.Value = null;//manager.GetObject("txtMa_Vt.Value");
            this.txtMa_Vt.Validated += new EventHandler(this.txtMa_Vt_Validated);
            this.lblMa_Vt.AutoSize = true;
            this.lblMa_Vt.FollowedSystemFont = false;
            this.lblMa_Vt.Font = new Font("Arial", 14.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblMa_Vt.ForeColor = Color.Blue;
            this.lblMa_Vt.Location = new Point(11, 0x51);
            this.lblMa_Vt.Name = "lblMa_Vt";
            this.lblMa_Vt.Size = new Size(0x6d, 0x16);
            this.lblMa_Vt.TabIndex = 0x67;
            this.lblMa_Vt.Text = "MẶT H\x00c0NG";
            this.lblNote01.BorderStyle = BorderStyle.Fixed3D;
            this.lblNote01.Font = new Font("Arial", 14.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblNote01.ForeColor = Color.Blue;
            this.lblNote01.Location = new Point(12, 0x71);
            this.lblNote01.Name = "lblNote01";
            this.lblNote01.Size = new Size(0x288, 0x1a);
            this.lblNote01.TabIndex = 0x68;
            this.lblNote02.BorderStyle = BorderStyle.Fixed3D;
            this.lblNote02.Font = new Font("Arial", 14.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblNote02.ForeColor = Color.Blue;
            this.lblNote02.Location = new Point(0x2a2, 0x71);
            this.lblNote02.Name = "lblNote02";
            this.lblNote02.Size = new Size(0x6c, 0x1a);
            this.lblNote02.TabIndex = 0x68;
            this.gBoxControl3.Font = new Font("Arial", 8.25f);
            this.gBoxControl3.Location = new Point(-5, 70);
            this.gBoxControl3.Name = "gBoxControl3";
            this.gBoxControl3.Size = new Size(0x324, 7);
            this.gBoxControl3.TabIndex = 0x6a;
            this.gBoxControl3.TabStop = false;
            this.gBoxControl4.Font = new Font("Arial", 8.25f);
            this.gBoxControl4.Location = new Point(-5, 0x68);
            this.gBoxControl4.Name = "gBoxControl4";
            this.gBoxControl4.Size = new Size(0x324, 7);
            this.gBoxControl4.TabIndex = 0x6a;
            this.gBoxControl4.TabStop = false;
            this.lblMa_Dt.AutoSize = true;
            this.lblMa_Dt.FollowedSystemFont = false;
            this.lblMa_Dt.Font = new Font("Arial", 14.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.lblMa_Dt.ForeColor = Color.Blue;
            this.lblMa_Dt.Location = new Point(0x1d3, 0x51);
            this.lblMa_Dt.Name = "lblMa_Dt";
            this.lblMa_Dt.Size = new Size(0x87, 0x16);
            this.lblMa_Dt.TabIndex = 0x67;
            this.lblMa_Dt.Text = "KH\x00c1CH H\x00c0NG";
            this.txtMa_Dt.CharacterCasing = CharacterCasing.Upper;
            this.txtMa_Dt.CheckTextSearched = false;
            this.txtMa_Dt.DisabledForeColor = Color.Blue;
            this.txtMa_Dt.FollowedSystemFont = false;
            this.txtMa_Dt.Font = new Font("Arial", 14.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.txtMa_Dt.Location = new Point(610, 0x4e);
            this.txtMa_Dt.Name = "txtMa_Dt";
            this.txtMa_Dt.OldValue = null;
            this.txtMa_Dt.Size = new Size(0xac, 0x1d);
            this.txtMa_Dt.TabIndex = 5;
            this.txtMa_Dt.TableName = "DMDT";
            this.txtMa_Dt.TabStop = false;
//            this.txtMa_Dt.Value = null;//manager.GetObject("txtMa_Dt.Value");
            this.txtMa_Vt_Temp.CharacterCasing = CharacterCasing.Upper;
            this.txtMa_Vt_Temp.CheckTextSearched = false;
            this.txtMa_Vt_Temp.DisabledForeColor = Color.Blue;
            this.txtMa_Vt_Temp.Font = new Font("Arial", 8.25f);
            this.txtMa_Vt_Temp.Location = new Point(0xb2, 0x52);
            this.txtMa_Vt_Temp.MaxLength = 0x20;
            this.txtMa_Vt_Temp.Name = "txtMa_Vt_Temp";
            this.txtMa_Vt_Temp.OldValue = null;
            this.txtMa_Vt_Temp.Size = new Size(0x7c, 20);
            this.txtMa_Vt_Temp.TabIndex = 4;
            this.txtMa_Vt_Temp.UserTextLength = AppInstance.TextControlLength.ItemCode;
//            this.txtMa_Vt_Temp.Value = null;//manager.GetObject("txtMa_Vt_Temp.Value");
            this.txtMa_Vt_Temp.Enter += new EventHandler(this.txtMa_Vt_Temp_Enter);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.ClientSize = new Size(0x31c, 0x216);
            base.Controls.Add(this.lblNote02);
            base.Controls.Add(this.lblNote01);
            base.Controls.Add(this.txtMa_Dt);
            base.Controls.Add(this.lblMa_Dt);
            base.Controls.Add(this.txtMa_Vt);
            base.Controls.Add(this.lblMa_Vt);
            base.Controls.Add(this.grdDetail);
            base.Controls.Add(this.gBoxControl4);
            base.Controls.Add(this.gBoxControl3);
            base.Controls.Add(this.txtMa_Vt_Temp);
            //base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FormCTKHOBarcodeEditor";
            base.Controls.SetChildIndex(this.txtMa_Vt_Temp, 0);
            base.Controls.SetChildIndex(this.gBoxControl3, 0);
            base.Controls.SetChildIndex(this.gBoxControl4, 0);
            base.Controls.SetChildIndex(this.grdDetail, 0);
            base.Controls.SetChildIndex(base.lblPhan_Loai, 0);
            base.Controls.SetChildIndex(base.txtSo_Ct, 0);
            base.Controls.SetChildIndex(base.txtNgay_Ps, 0);
            base.Controls.SetChildIndex(base.txtNgay_Ct, 0);
            base.Controls.SetChildIndex(base.txtMa_Loai1, 0);
            base.Controls.SetChildIndex(base.txtMa_Loai2, 0);
            base.Controls.SetChildIndex(base.txtMa_Loai3, 0);
            base.Controls.SetChildIndex(this.lblMa_Vt, 0);
            base.Controls.SetChildIndex(this.txtMa_Vt, 0);
            base.Controls.SetChildIndex(this.lblMa_Dt, 0);
            base.Controls.SetChildIndex(this.txtMa_Dt, 0);
            base.Controls.SetChildIndex(this.lblNote01, 0);
            base.Controls.SetChildIndex(this.lblNote02, 0);
            ((ISupportInitialize) this.grdDetail).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void LotNumberCalculate(DataRow po_LocalItemRow, object ps_LocalItemCode, decimal pi_QuantityAdded)
        {
            if (po_LocalItemRow != null)
            {
                DataTable table = base.AppInvoiceParameters.MakeStockLotQtyTable(this.BarcodePosted, base.SavedMasterOrder, base.txtNgay_Ct.Value, this.Barcode_Ma_Kho, ps_LocalItemCode, base.LocalDetailTable);
                table.PrimaryKey = new DataColumn[] { table.Columns["Lo_Hang"] };
                DataColumn column = new DataColumn("OutQty", typeof(decimal));
                table.Columns.Add(column);
                if ((table != null) && (table.Rows.Count != 0))
                {
                    DataRow row;
                    BindingSource dataSource = base.DetailGridBrowser.DataSource as BindingSource;
                    foreach (DataRow row2 in base.LocalDetailTable.Rows)
                    {
                        if (((row2.RowState != DataRowState.Detached) && (row2.RowState != DataRowState.Deleted)) && row2["Ma_Vt"].Equals(ps_LocalItemCode))
                        {
                            row = table.Rows.Find(row2["Lo_Hang"]);
                            if (row != null)
                            {
                                if (((decimal) row["So_Luong"]) >= pi_QuantityAdded)
                                {
                                    row2["So_Luong9"] = ((decimal) row2["So_Luong9"]) + pi_QuantityAdded;
                                    row2["So_Luong"] = row2["So_Luong9"];
                                    pi_QuantityAdded = 0M;
                                    dataSource.Position = dataSource.Find(base.DetailOrderString, row2[base.DetailOrderString]);
                                    return;
                                }
                                row2["So_Luong9"] = ((decimal) row2["So_Luong9"]) + ((decimal) row["So_Luong"]);
                                row2["So_Luong"] = row2["So_Luong9"];
                                pi_QuantityAdded -= (decimal) row["So_Luong"];
                                table.Rows.Remove(row);
                            }
                        }
                    }
                    int key = 0;
                    decimal num2 = 0M;
                    foreach (DataRow row4 in base.LocalDetailTable.Rows)
                    {
                        if ((row4.RowState != DataRowState.Deleted) && (((int) row4["Stt_Nv"]) > key))
                        {
                            key = (int) row4["Stt_Nv"];
                        }
                    }
                    while ((pi_QuantityAdded > 0M) && (table.Rows.Count > 0))
                    {
                        row = table.Rows[0];
                        if (((decimal) row["So_Luong"]) >= pi_QuantityAdded)
                        {
                            num2 = pi_QuantityAdded;
                            pi_QuantityAdded = 0M;
                        }
                        else
                        {
                            num2 = (decimal) row["So_Luong"];
                            pi_QuantityAdded -= num2;
                        }
                        DataRow row3 = base.MakeDetailRow();
                        key++;
                        if (po_LocalItemRow["Gia_Ban"] != DBNull.Value)
                        {
                            decimal decimal1 = (decimal) po_LocalItemRow["Gia_Ban"];
                        }
                        row3["Stt_Nv"] = key;
                        row3["He_So9"] = 1;
                        row3["Ma_Vt"] = ps_LocalItemCode;
                        row3["Ten_Vt"] = po_LocalItemRow["Ten_Vt"];
                        row3["Dvt"] = po_LocalItemRow["Dvt"];
                        row3["Ma_Kho"] = this.Barcode_Ma_Kho;
                        row3["So_Luong9"] = num2;
                        row3["So_Luong"] = num2;
                        row3["Lo_Hang"] = row["Lo_Hang"];
                        if (AppInstance.LocationCtrl)
                        {
                            row3["Ma_Ke"] = row["Ma_Ke"];
                        }
                        if (AppInstance.DeadLineCtrl)
                        {
                            row3["Han_Dung"] = row["Han_Dung"];
                        }
                        if (AppInstance.QualityCtrl)
                        {
                            row3["H_Luong"] = row["H_Luong"];
                        }
                        else
                        {
                            row3["H_Luong"] = 0;
                        }
                        if (AppInstance.DampCtrl)
                        {
                            row3["Do_Am_PT"] = row["Do_Am_PT"];
                        }
                        else
                        {
                            row3["Do_Am_PT"] = 0;
                        }
                        if (AppInstance.AlcoholCtrl)
                        {
                            row3["Do_Con_PT"] = row["Do_Con_PT"];
                        }
                        else
                        {
                            row3["Do_Con_PT"] = 0;
                        }
                        table.Rows.Remove(row);
                        base.LocalDetailTable.Rows.Add(row3);
                    }
                    dataSource.Position = dataSource.Find(base.DetailOrderString, key);
                    if (pi_QuantityAdded > 0M)
                    {
                        MessageBox.Show("Không đủ hàng hóa để xuất!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
        }

        internal override void OnAdd()
        {
            object obj2 = base.LocalMasterRow["So_Ct"];
            base.LocalMasterRow = base.MakeMasterRow();
            base.LocalMasterRow["Posted"] = (this.BarcodePosted >= 1) ? 2 : 0;
            base.LocalMasterRow["Locked"] = this.BarcodeLocked;
            base.LocalMasterRow["Nh_Ct"] = base.AppInvoiceParameters.InvoiceType;
            base.LocalMasterRow["So_Ct"] = this.oMethod.Cong_So_Ct(AppInstance.Subsidiary, base.InvoiceCode, base.LocalMasterRow["Ngay_Ct"], obj2);
            base.LocalMasterRow["Ma_Dt"] = this.Barcode_Ma_Dt;
            base.LocalMasterRow["Dien_Giai"] = this.Barcode_Dien_Giai;
            base.LocalMasterRow["Desc_EN"] = this.Barcode_Desc_EN;
            base.LocalDetailTable.Rows.Clear();
            this.lblNote01.Text = "";
            this.lblNote02.Text = "";
            this.txtMa_Vt.Focus();
            base.OnAdd();
        }

        internal override void OnResetControls()
        {
            base.DetailGridBrowser = this.grdDetail;
            if (!AppInstance.LocationCtrl)
            {
                base.DetailGridBrowser.Columns[3].Visible = false;
            }
            if (!AppInstance.LotNumberCtrl)
            {
                base.DetailGridBrowser.Columns[4].Visible = false;
            }
            if (!AppInstance.DeadLineCtrl)
            {
                base.DetailGridBrowser.Columns[5].Visible = false;
            }
            if (base.AppInvoiceParameters.FreeFields <= 0)
            {
                int height = base.txtMa_Loai1.Height;
                this.grdDetail.Height += height;
            }
            base.OnResetControls();
        }

        protected override bool OnSave()
        {
            if (base.txtNgay_Ps.Value == DBNull.Value)
            {
                MessageBox.Show("Chưa nhập ngày phát sinh!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (base.txtNgay_Ct.Value == DBNull.Value)
            {
                MessageBox.Show("Chưa nhập ngày chứng từ!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (((DateTime) base.txtNgay_Ct.Value) < AppInstance.OpeningDateOfSubEnterprice)
            {
                MessageBox.Show("Ngày chứng từ phải từ ngày " + AppInstance.OpeningDateOfSubEnterprice.ToShortDateString(), AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (((DateTime) base.txtNgay_Ct.Value) <= AppInstance.EndingDateOfSubEnterprice)
            {
                MessageBox.Show("Ngày chứng từ phải từ ngày " + AppInstance.EndingDateOfSubEnterprice.AddDays(1.0).ToShortDateString(), AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return false;
            }
            if (this.oMethod.NumberExisted(AppInstance.Subsidiary, base.CheckNewRow, base.SavedMasterOrder, base.InvoiceCode, base.txtNgay_Ct.Value, base.txtSo_Ct.Value))
            {
                object obj2 = base.txtSo_Ct.Value;
                base.txtSo_Ct.Value = this.oMethod.Cong_So_Ct(AppInstance.Subsidiary, base.InvoiceCode, base.LocalMasterRow["Ngay_Ct"], obj2);
            }
            base.DetailGridBrowser.EndEdit();
            base.LocalMasterRow["Ngay_Ps"] = base.txtNgay_Ps.Value;
            base.LocalMasterRow["Ngay_Ct"] = base.txtNgay_Ct.Value;
            base.LocalMasterRow["So_Ct"] = base.txtSo_Ct.Value;
            base.LocalMasterRow["Ma_Dt"] = this.txtMa_Dt.Value;
            base.LocalMasterRow["Ma_Loai1"] = base.txtMa_Loai1.Value;
            base.LocalMasterRow["Ma_Loai2"] = base.txtMa_Loai2.Value;
            base.LocalMasterRow["Ma_Loai3"] = base.txtMa_Loai3.Value;
            if (this.oMethod.Save(base.AppInvoiceParameters.InvoiceHistory, base.CheckNewRow, AppInstance.Subsidiary, AppInstance.UserName, base.LocalMasterRow, base.LocalDetailTable))
            {
                base.AppPrintLib.OnBarcodeOutInventoryPrint(base.AppInvoiceParameters, base.LocalMasterRow, base.LocalDetailTable);
                this.txtMa_Vt.Value = DBNull.Value;
                return base.OnSave();
            }
            return false;
        }

        internal override void OnShow()
        {
            base.OnShow();
            this.txtMa_Dt.Value = base.LocalMasterRow["Ma_Dt"];
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Return)
            {
                this.UserKeyPassed = false;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void txtMa_Vt_Temp_Enter(object sender, EventArgs e)
        {
            this.txtMa_Vt.Focus();
        }

        private void txtMa_Vt_Validated(object sender, EventArgs e)
        {
            this.AddNewRow(((TextControl) sender).Value);
        }

        private object Barcode_Desc_EN
        {
            get
            {
                return this.is_Barcode_Desc_EN;
            }
            set
            {
                this.is_Barcode_Desc_EN = value;
            }
        }

        private object Barcode_Dien_Giai
        {
            get
            {
                return this.is_Barcode_Dien_Giai;
            }
            set
            {
                this.is_Barcode_Dien_Giai = value;
            }
        }

        private object Barcode_Ma_Dt
        {
            get
            {
                return this.is_Barcode_Ma_Dt;
            }
            set
            {
                this.is_Barcode_Ma_Dt = value;
            }
        }

        private object Barcode_Ma_Kho
        {
            get
            {
                return this.is_Barcode_Ma_Kho;
            }
            set
            {
                this.is_Barcode_Ma_Kho = value;
            }
        }

        private bool BarcodeLocked
        {
            get
            {
                return this.ib_BarcodeLocked;
            }
            set
            {
                this.ib_BarcodeLocked = value;
            }
        }

        private int BarcodePosted
        {
            get
            {
                return this.ii_BarcodePosted;
            }
            set
            {
                this.ii_BarcodePosted = value;
            }
        }

        private bool UserKeyPassed
        {
            get
            {
                return this.ib_UserKeyPassed;
            }
            set
            {
                this.ib_UserKeyPassed = value;
            }
        }
    }
}

